<template>
  <div class="login-container">
    <van-nav-bar class="page-nav-bar" title="信阳头条-登录" />

    <van-form @submit="login">
      <van-field
        v-model="form.mobile"
        name="手机号"
        label="手机号"
        placeholder="输入手机号"
        required
        :rules="rules.mobile"
      />
      <van-field
        v-model="form.code"
        type="password"
        name="密码"
        label="验证码"
        placeholder="输入请验证码"
        required
        :rules="rules.code"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api/userAPI'
import { mapMutations } from 'vuex'
export default {
  data() {
    return {
      form: {
        mobile: '13888888888',
        code: '246810'
      },
      rules: {
        mobile: [
          { required: true, message: '请填写您的手机号', trigger: 'onBlur' },
          {
            pattern: /^1\d{10}$/,
            message: '请填写正确的手机号',
            trigger: 'onBlur'
          }
        ],
        code: [{ required: true, message: '请填写您的密码', trigger: 'onBlur' }]
      }
    }
  },

  components: {},

  computed: {},

  mounted() {},

  methods: {
    ...mapMutations(['saveTokenInfo']),
    async login() {
      // loginAPI(this.form).then(function (res) {
      //   console.log('res:', res)
      // })
      const { data: res } = await loginAPI(this.form)
      this.saveTokenInfo(res.data)
      // this.$store.commit('saveTokenInfo', res.data)
      if (res.message === 'OK') {
        if (this.$route.query.pre) {
          this.$router.push(this.$route.query.pre)
        } else {
          this.$router.push('/')
        }
        // this.$router.push('/')
      }
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  // padding-top: 46px;
}
</style>
